package org.mtmi.gestures;

import org.eclipse.swt.widgets.Display;
import org.mtmi.tuio.Trace;
import org.mtmi.tuio.CursorPoint.States;
import org.mtmi.tuio.listeners.DragListener;
import org.mtmi.tuio.listeners.TUIODragEvent;
import org.mtmi.ui.controls.ITouchableControl;

public class DragGesture implements IGesture {

	public ITouchableControl target;
    public Trace trace;
	
	
	public static boolean detectFromTrace(Trace trace) {
		// If cursor moved this is a drag ! 
		if (trace.getLast().getM_state() == States.UPDATED) {
			return true;
		}
		
		return false;
	}

	public DragGesture(Trace trace) {
		this.target = trace.getInitialTarget();
		this.trace = trace;
	}

	@Override
	public GestureType getGestureType() {
		return GestureType.DRAG_GESTURE;
	}

	@Override
	public String getName() {
		return "DragGesture";
	}

	@Override
	public void notifyTarget() {
		if (this.target instanceof DragListener) {
			
			
			final DragListener finalTarget = (DragListener) target;
			
			Display.getDefault().asyncExec(new Runnable() {
				
				@Override
				public void run() {
					
					finalTarget.drag(new TUIODragEvent(trace.getLast().getX(), trace.getLast().getY()));
					
				}
			});
		}
		
	}

}
